﻿<%@ Import Namespace="dbPoll_Application.Models" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<KeyValuePair<Question, IDictionary<String, IDictionary<int,int>>>>" %>


    <script type="text/javascript">

    // Build the array of data
    var dataArray<%=Model.Key.questionID%> = [];
    dataArray<%=Model.Key.questionID%>[0] = ['Answer', 'Responses'];

    <% 
        int responseNum = 0;
        
    
        // Get the chart
        QuestionChart chart = Model.Key.resultsChart;
        
        String divide = "";
        String format = "";
                int i = 1;
        IList<Entity> selectedEntities = (IList<Entity>)ViewData["selectedEntities"];
        
        foreach (var entity in selectedEntities)
        {
        
        // Get the total count of responses
        foreach (var question in Model.Value)
        { 
            responseNum += question.Value[entity.entityID];
        }
        

        // Get chart value type  
        if (chart.valueType.Equals("Percentage"))
        {
            divide = "/" + responseNum;
            format = ", format:'#%'";
        }
    

        

        // For each response
        foreach (var question in Model.Value)
        {
            String correct = "";
            // Check if it's correct
            if (Model.Key.getCorrect() != null) 
            {
                foreach (var correctAnswer in Model.Key.getCorrect())
                {
                    if (question.Key.Equals(correctAnswer)) 
                    {
                        correct = "✔ "; 
                    }
                }
            }
            
    %> 
        dataArray<%=Model.Key.questionID%>[<%=i%>] = ["<%=correct%><%=question.Key%>:<%=entity.name %>", <%=question.Value[entity.entityID]%><%=divide%>];
    <% 
        i++;
        }
        }

    // Get question chart colours for each chart
    String chartType = chart.GetQuestionChartType();
    String[] coloursArray = chart.colours.Split(new char[] { ',', ' ' });

    String chartColours = "";
            int j = 0;
            foreach (String colour in coloursArray)
            {
                if (colour != "")
                {
                    if (colour.Equals("orange")) chartColours += "'#FFA500'";
                    else chartColours += "'" + colour + "'";
                    if (j < coloursArray.Length - 2)
                    {
                        chartColours += ", ";
                    }
                    j++;
                }
            }  
      
      
    %>

//    alert('<%=Model.Key.questionID%>');
        var data<%=Model.Key.questionID%> = google.visualization.arrayToDataTable(dataArray<%=Model.Key.questionID%>);
        
         <%   
            // Set formatted value for each of the rows (if percentage)
            if (chart.valueType.Equals("Percentage"))
            {
            foreach (var entity in selectedEntities)
                {
                int m = 0;
                foreach (var question in Model.Value)
                {
                    %>
                    data<%=Model.Key.questionID%>.setFormattedValue(<%=m%>, 1, "<%=question.Value[entity.entityID]%>"); 
                    <%
                    m++;
                }
                }
            }
         %>
        
        // Instantiate and draw our chart, passing in some options.
        var chart<%=Model.Key.questionID%> = new google.visualization.<%=Model.Key.resultsChart.GetQuestionChartType()%>(document.getElementById('chart_div<%=Model.Key.questionID%>'));
        
        chart<%=Model.Key.questionID%>.draw(data<%=Model.Key.questionID%>, {width: 420, height: 360, title: "<%=Model.Key.question%>", 
                                titleTextStyle: {fontSize: 10}, colors: [<%=chartColours%>],
                                hAxis: {title: dataArray<%=Model.Key.questionID%>[0][0]<%=format%>}, vAxis: {title: dataArray<%=Model.Key.questionID%>[0][1]<%=format%>} });
    

    </script>
    
<script>
            var dataTable = new google.visualization.DataTable();
            dataTable.addColumn('string');
            dataTable.addColumn('number');
            dataTable.addRows([
            <% 
               String inColours = "";
                foreach (String colour in coloursArray)
                {
                    if (colour != "")
                    {
                        if (colour.Equals("orange")) inColours += "FFA500";
                        else if (colour.Equals("green")) inColours += "008000";
                        else if (colour.Equals("yellow")) inColours += "FFFF00";
                        else inColours +="FF0000";
                        inColours += "|";
                    }
                }  
                inColours=inColours.Substring(0,inColours.Length-1);
               
               float max=0;
               String stuff="";
               int z = 0;
               foreach (var entity in selectedEntities)
               {
                   i = 0;
               if (!chart.valueType.Equals("Percentage")) 
               {
                   foreach (var answer in Model.Value) 
                   {%> ['Choice <%=i+1 %>',<%if (answer.Value[entity.entityID]==0 && !chartType.Equals("PieChart")){%>NaN]<%}else{ %><%=answer.Value[entity.entityID]%>]<%} 
                   stuff+= "Choice"+(i+1)+"|"; i++;
                    if (i!=Model.Value.Count) {%>,<%}
                    if (max<answer.Value[entity.entityID]) max=answer.Value[entity.entityID];
                   }
               }
               else 
               {
                   float total=0;                   
                   foreach (var answer in Model.Value) total+=answer.Value[entity.entityID];
                   foreach (var answer in Model.Value)
                   {%> ['Choice <%=i+1 %>',<%=answer.Value[entity.entityID]/total*100%>]<% stuff+= "Choice"+(i+1)+"|"; i++;
                    if (i!=Model.Value.Count) {%>,<%} 
                    if (max<answer.Value[entity.entityID]/total*100) max=answer.Value[entity.entityID]/total*100;
                   }
               }
               z++;
               if (z != selectedEntities.Count)
               { %>,<% }
               }
                
               stuff=stuff.Substring(0,stuff.Length-1);
               if (chartType.Equals("ColumnChart")) chartType="bvs";
               else if (chartType.Equals("BarChart")) chartType="bhs";
               else chartType="p";
               %>]);
               
            var options = { cht: '<%=chartType%>', chs: '250x220', <%if (!chartType.Equals("p")) {%>
                            chbh:'r,0.5,1.5',<%if (chartType.Equals("bvs")){%>chxt:'x,x,y,y',
                            chxl:'0:|<%for (int c=0; c<Model.Value.Count * selectedEntities.Count;c++) {%><%=c+1%>|<%}%>1:|Answer|3:|Responses <%if (chart.valueType.Equals("Percentage")){%>(in %)<%}%>|',<%} 
                            else {%>chxt:'y,y,x,x',
                            chxl:'0:|<%for (int c=Model.Value.Count * selectedEntities.Count; c>0;c--) {%><%=c%>|<%}%>1:|Answer|3:|Responses <%if (chart.valueType.Equals("Percentage")){%>(in %)<%}%>|',<%} %>
                            chxr: '1,0,<%=Model.Value.Count%>|2,0,<%=max%>|3,0,<%=max%>',
                            chxp:'1,<%=Model.Value.Count/2.0%>|3,<%=max/2.0%>', 
                            <%}
                            else {%>chl:'<%=stuff %>',chdl:'',chdlp:'',<%}%>chxs:'0,000000,11|1,000000,13|2,000000,11|3,000000,13',
                            chco: '<%=inColours %>'};

            var chart = new google.visualization.ImageChart(document.getElementById('Image_div<%=Model.Key.questionID%>'));
            chart.draw(dataTable, options);
            var url = chart.getImageUrl().replace("&chdl&chdlp","");
            url = url.replace("chs=250x220","chs=320x220");
            <%if (chartType.Equals("p")) {%> document.getElementById('urls').value = document.getElementById('urls').value + url +','; <%}
            else {%>document.getElementById('urls').value = document.getElementById('urls').value + url+',';<%} %>

</script>
    <!--Div to hold the chart-->
    <div style="width:0;height:0; visibility:hidden" id="Image_div<%=Model.Key.questionID%>"></div>
    <div id="chart_div<%=Model.Key.questionID%>"></div>